//
//  ZYPlayerViewController.m
//  A Learning Walk
//
//  Created by 张勇 on 16/6/17.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "ZYPlayerViewController.h"
#import "findDetailViewController.h"
#import "CFDynamicLabel.h"
//音乐播放头文件
#import <AVFoundation/AVFoundation.h>
@interface ZYPlayerViewController ()//<AVAudioPlayerDelegate>//签协议


@property(nonatomic,copy)NSString *musicString;
@property(nonatomic,retain)CFDynamicLabel* ScrollLabel;//标记第几首


@end

@implementation ZYPlayerViewController



-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.translucent = NO;
    
    [self.playB setImage:[[UIImage imageNamed:@"toolbar_play_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if ([[defaults objectForKey:@"play"] isEqualToString:@"1"]) {
            [self.playB setImage:[[UIImage imageNamed:@"toolbar_pause_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        }else{
    [self.playB setImage:[[UIImage imageNamed:@"toolbar_play_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    }

    
    //设置button的背景图片
    [self.playListB setImage:[[UIImage imageNamed:@"toolbar_playinglist_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.backB setImage:[[[UIImage imageNamed:@"toolbar_prev_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.playB setImage:[[UIImage imageNamed:@"tabbar_np_playnon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.goB setImage:[[UIImage imageNamed:@"toolbar_next_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.timingB setImage:[[UIImage imageNamed:@"toolbar_clock_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    

    
    //赋值
    self.userImageV.layer.masksToBounds = YES;
    self.userImageV.layer.cornerRadius = self.userImageV.frame.size.width / 2;
    [self.userImageV sd_setImageWithURL:[NSURL URLWithString:self.model.smallLogo] placeholderImage:[UIImage imageNamed:@"holderr"]];
    self.userL.text = self.model.nickname;
    self.userL.font = [UIFont systemFontOfSize:15];
    [self.BGImageV sd_setImageWithURL:[NSURL URLWithString:self.model.coverLarge] placeholderImage:[UIImage imageNamed:@"hold"]];
    
    self.timeTotleL.textColor = [UIColor orangeColor];
    if (self.model.duration.integerValue % 60 < 10) {
        self.timeTotleL.text = [NSString stringWithFormat:@"%ld:%ld",self.model.duration.integerValue / 60 , self.model.duration.integerValue % 60 * 10];
    }else{
        self.timeTotleL.text = [NSString stringWithFormat:@"%ld:%ld",self.model.duration.integerValue / 60 , self.model.duration.integerValue % 60 ];
        self.timeTotleL.font = [UIFont systemFontOfSize:10];
    }

    
    //得到传过来的下标
    NSInteger index = self.indexRow;
    //打开直接播放
    NSString *string = self.AVArray[index];
    self.musicString = string;
    NSLog(@"%ld",(long)index);
    [self.player play:self.musicString];
    [self playMusic];
    
   
    //创建滚动条
    self.ScrollLabel.text = nil;
    self.ScrollLabel = [[CFDynamicLabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - (self.view.frame.size.width / 4) , 20, self.view.frame.size.width / 2, 40)];
    self.ScrollLabel.speed = 0.6;
    
    [self.effectView addSubview:self.ScrollLabel];
    self.ScrollLabel.text = self.model.title;
    self.ScrollLabel.textColor = [UIColor colorWithRed:arc4random()%256 / 255.0 green:arc4random()%256 / 255.0 blue:arc4random()%256 / 255.0 alpha:1];
    self.ScrollLabel.font = [UIFont systemFontOfSize:17];
    self.ScrollLabel.backgroundColor = self.effectView.backgroundColor;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"playingback_h"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
   
    

    
    //下面的初始化也可以,但设置不了enableVolumeMixer,就无法调节音量
    self.player = [[STKAudioPlayer alloc] init];
    
    self.player.volume = 0.5;
    self.player.meteringEnabled = YES;//是否启用音频测量，默认为NO，一旦启用音频测量可以通过updateMeters方法更新测量值
    //设置slider默认值
    self.progressSlider.value = 0;
    
   
    
    //NSLog(@"%@",self.model);
    // Do any additional setup after loading the view from its nib.
}




//设置slider和播放时间相等
-(void)timerAction{
    //设置slider等于当前时间
    // self.progressSlider.value = self.player.currentTime;
    NSLog(@"计时器");
}
//进度条
- (IBAction)progressSlider:(id)sender {
    
    NSLog(@"进度条");
}

//下一曲
- (IBAction)GoButton:(UIButton *)sender {
    
    NSLog(@"下一曲");
    //+1后对总数取余是为了防止点击多次,数组越界
    self.indexRow++;

    if (self.indexRow > self.AVArray.count - 1) {
        self.indexRow = 0;
        NSString *string = [self.AVArray objectAtIndex:self.indexRow];
        [self.player play:string];
        [self playMusic];
    }else{
        NSString *string = [self.AVArray objectAtIndex:self.indexRow];
        [self.player play:string];
        [self playMusic];
        
    }
    
    NSLog(@"indexRow = %ld", self.indexRow);
//    //如果歌曲总数大于当前播放的音乐下标

    
}


//不播放列表
- (IBAction)playListB:(UIButton *)sender {
    
}
////上一曲
- (IBAction)backB:(UIButton *)sender {
        self.indexRow--;
       
        if (self.indexRow < 0) {
            self.indexRow = self.AVArray.count - 1;
            NSString *string = [self.AVArray objectAtIndex:self.indexRow];
            [self.player play:string];
            [self playMusic];
        
        }else{
            NSString *string = [self.AVArray objectAtIndex:self.indexRow];
            [self.player play:string];
            [self playMusic];
        }
    NSLog(@"上一曲");
}
//播放按钮
- (IBAction)playB:(UIButton *)sender {
    
    NSLog(@"222222%d", self.player.state);
    
    if (self.player.state == STKAudioPlayerStatePaused) {
        
        NSLog(@"播放");
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:@"0" forKey:@"play"];
         [[NSUserDefaults standardUserDefaults] synchronize];
        [self.playB setImage:[[UIImage imageNamed:@"toolbar_pause_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self.player resume];//继续
        
    }else if (self.player.state == STKAudioPlayerStatePlaying) {
        
        NSLog(@"暂停");
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:@"1" forKey:@"play"];
         [[NSUserDefaults standardUserDefaults] synchronize];
        [self.playB setImage:[[UIImage imageNamed:@"toolbar_play_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self.player pause];
        
    }else
    {
        NSLog(@"继续播吧");
        [self.player play:self.musicString];
        [self playMusic];
        [self ButtonPlay];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:@"0" forKey:@"play"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
    }
    
}
//播放状态
-(void)ButtonPlay{

[self.playB setImage:[[UIImage imageNamed:@"toolbar_pause_n_p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

}
//播放音乐
- (void)playMusic
{
    [self ButtonPlay];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(track) userInfo:nil repeats:YES];
    self.ScrollLabel.text = self.model.title;
}
-(void)track
{
   // NSLog(@"====%@",self.player);
    
    self.progressSlider.maximumValue = self.player.duration;//音乐总共时长
    self.progressSlider.value = self.player.progress;//当前进度
    
    //当前时长progress
    NSInteger proMin = (NSInteger)self.player.progress / 60;//当前秒
    NSInteger proSec = (NSInteger)self.player.progress % 60;//当前分钟
    
    self.timeNowL.text = [NSString stringWithFormat:@"%02ld:%02ld", proMin, proSec];
    self.timeNowL.font = [UIFont systemFontOfSize:10];
    self.timeNowL.textColor = [UIColor orangeColor];
    // NSLog(@"proMin = %ld proSec = %ld",proMin, proSec);
    
    
}

//计时器
- (IBAction)timingB:(UIButton *)sender {
    
}

//返回按钮
-(void)backAction:(UIBarButtonItem *)bar{

[self dismissViewControllerAnimated:YES completion:^{
    
    
}];
}



+(instancetype)shareZYPlayerViewController
{
    
    static ZYPlayerViewController *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        player = [[ZYPlayerViewController alloc] init];
        
    });
    
    return player;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dealloc {
    [_userImageV release];
    [_userL release];
    [_BGImageV release];
    [_timeNowL release];
    [_timeTotleL release];
    [_playListB release];
    [_backB release];
    [_playB release];
    [_goB release];
    [_timingB release];
    [_AVArray release];
    [_ScrollLabel release];
    [_effectView release];
    [super dealloc];
}



@end
